var edit=-1;
$(function(){
	$('#ordersgrid').datagrid({
	columns:[[
		    {field:'goodsuuid',title:'商品编号',width:100,editor:{type:'numberbox',
		     options:{
		    	disabled:true	
		    	}}},
  		    {field:'goodsname',title:'商品名称',width:100,editor:{type:'combobox',
  		    options:{
  		    	url:'product_getlist',
  		        valueField:'pname',
  		        textField:'pname',
  		        onSelect:function(goods){
  	  		     var goodsuuid=getEditor('goodsuuid');	
  	  		     $(goodsuuid.target).val(goods.pid);
   		        var priceedit=getEditor('price');	
  		        if(Request['TYPE']*1==1){//采购订单
  		        //设置为进货价格
  		        $(priceedit.target).val(goods.market_price);  		        	
  		        }
  		        var numEditor=getEditor('num');
  		        $(numEditor.target).select();
  		        bindGridEditor();
  		        cal();
  		        sum();
  		        }
  		     }	
  		    }},
  		    {field:'price',title:'价格',width:100,editor:{type:'numberbox',options:{precision:2,prefix:'￥'}}},
  		    {field:'num',title:'数量',width:100,editor:'numberbox'},
  		    {field:'money',title:'金额',width:100,editor:{type:'numberbox',options:{precision:2,prefix:'￥'}}},
	        {field:'-',title:'操作',width:100,formatter:function(val,row,rowindex){
	        	 if(row.num!="合计")
	        	return '<a href="javascript:void(0)" onclick="deleteRow('+rowindex+')">删除</a>'; 
	         }}
  		    ]]	,
	     singleSelect:true,
	     rownumbers:true,
	     showFooter:true,
	     toolbar:[
	    	 {
	    		 text:'新增',
	    		 iconCls:'icon-edit',
	    		 handler:function(){
	    			 if(edit>-1)
	    			 $('#ordersgrid').datagrid('endEdit',edit);
	    			 $('#ordersgrid').datagrid('appendRow',{num:0,money:0});
	    		     var rows=$('#ordersgrid').datagrid('getRows');
	    			 edit=rows.length-1;
	    		     $('#ordersgrid').datagrid('beginEdit',rows.length-1);
	    		 }
	    	 },'-',{
	    		 text:'提交',
	    		 iconCls:'icon-save',
	    		 handler:function(){
	    			 //关闭编辑状态
	    			 if(edit>-1)
	    			 $('#ordersgrid').datagrid('endEdit',edit);
	    			 //获取所有的信息
	    			 var rows=$('#ordersgrid').datagrid('getRows');
	    			 if(rows.length<=0)return ;
	    			 
	    			 var formdata=$('#orderForm').serializeJSON();
	    			 console.log(formdata);
	    			 //转成json字符串
	    			 formdata.json=JSON.stringify(rows);
	    			 $.ajax({
	    			   url:'orders_add',
	    			   data:formdata,
	    			   dataType:'json',
	    			   type:'post',
	    			   success:function(res){
	    				  $.messager.alert('提示',res.message,'info',function(){
	    				if(res.success){
	    					  //清空供应商
	    					$('#supplier').combogrid('clear');  
	    					//清空表格  
	    					$('#ordersgrid').datagrid('loadData',{total:0,rows:[],footer:[{num:'合计',money:0}]});
	    					//关闭增加订单的窗口
	    					$('#addOrdersDlg').dialog('close');
	    					//刷新列表
	    					$('#grid').datagrid('reload');
	    				   }
	    				  }) 
	    			   }
	    			 });
	    			 
	    		 }	 
	    	 }
	     ],
	     onClickRow:function(rowindex,rowData){
	    	// rowindex:行下标
	    	 //rowData：点击行的数据
	    	 $("#ordersgrid").datagrid('endEdit',edit);
	    	 edit=rowindex;
	    	 $("#ordersgrid").datagrid('beginEdit',edit);
	     }
	});
	$('#ordersgrid').datagrid('reloadFooter',
			[{num:'合计',money:0}]);
	
	//加载供应商下拉表格
	$('#supplier').combogrid({
	 panelWidth:700,
	 idField:'uuid',
	 textField:'name',
	 url:'supplier_list?type='+Request['TYPE'],	
	 columns:[[
		    {field:'uuid',title:'编号',width:100},
  		    {field:'name',title:'名称',width:100},
  		    {field:'address',title:'联系地址',width:100},
  		    {field:'contact',title:'联系人',width:100},
  		    {field:'tele',title:'联系电话',width:100},
  		    {field:'email',title:'邮件地址',width:100},
	 ]],
	 mode:'remote'
	});
	
});

function getEditor(f){
 return $("#ordersgrid").datagrid('getEditor',{index:edit,field:f});
	
}

function cal(){
	 var numEdit=getEditor('num');
	 var num=$(numEdit.target).val();
	 
	 var priceEditor=getEditor('price');
	 var price=$(priceEditor.target).val();
	 
	 var money=num*price;
	 money=money.toFixed(2);
	 var moneyEditor=getEditor('money');
	 $(moneyEditor.target).val(money);
	 
	 //更新表格中的数据
	 $('#ordersgrid').datagrid('getRows')[edit].money=money;
	}

function bindGridEditor(){
	var numEditor=getEditor('num');
	$(numEditor.target).bind('keyup',function(){
		cal();
		sum();
	})
	
	var priceEditor=getEditor('price');
	$(priceEditor.target).bind('keyup',function(){
		cal();
		sum();
	})
}

function sum(){
	var rows=$('#ordersgrid').datagrid('getRows');
	var total=0;
	$.each(rows,function(i,row){
		total+=parseFloat(row.money);
	});
	total=total.toFixed(2);
	$('#ordersgrid').datagrid('reloadFooter',[{num:'合计',money:total}]);
}

function deleteRow(index)
{
	$('#ordersgrid').datagrid('endEdit',edit);
	
	$('#ordersgrid').datagrid('deleteRow',index);
	
	var data=$('#ordersgrid').datagrid('getData');
	$('#ordersgrid').datagrid('loadData',data);
	sum();
	
}
